# uv pip install torch 

import torch
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
from torchvision.transforms import ToTensor


import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets
import numpy

import torch  


import array


'''

x = torch.arange(1., 6.)
x
value,index=torch.topk(x, 3)
print(value,index)



x = torch.randn(3, 4)
sorted, indices = torch.sort(x)
print(sorted,indices)




a = torch.tensor([9.7, float('nan'), 3.1, float('nan')])
b = torch.tensor([-2.2, 0.5, float('nan'), float('nan')])
output=torch.fmax(a, b)
print(output)

output = torch.unique(torch.tensor([1, 3, 2, 3], dtype=torch.long))
print(output)


x = torch.randn(4)
x
y = torch.randn(4)
y
torch.dist(x, y, 3.5)
torch.dist(x, y, 3)
torch.dist(x, y, 0)
torch.dist(x, y, 1)


a = torch.randn(1, 3)
a
output=torch.max(a)
print(output)


a = torch.randn(4, 4)
print(a)
output=torch.amax(a, 0)
print(output)


a = torch.randn(4, 4)
a
output=torch.argmax(a)
print(output)


a = torch.tensor([0.7, -1.2, 0., 2.3])
a
torch.sign(a)


# Save to file

a = torch.full((2,2),2)
a
output=torch.pow(a, 2)

print(output)



a = torch.randn(3)
a
output=torch.mul(a, 100)
print(output)

b = torch.randn(4, 1)
b
c = torch.randn(1, 4)

c=c@b

print(c)
c
output=torch.mul(b, c)

print(output)

 

x = torch.tensor([ 0.3810,  1.2774, -0.2972, -0.3719,  0.4637])
output=torch.div(x, 0.5)
  
print(output)



a = torch.randn(4)
a
torch.asin(a)


x = torch.tensor([1.], requires_grad=True)
y = x ** 2
y.backward()

print(x.grad)



# Save to io.BytesIO buffer
 





x = torch.randn(3, 2)
y = torch.ones(3, 2)
x
torch.where(x > 0, 1.0, 0.0)
output=torch.where(x > 0, x, y)
print(output)



a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
torch.vstack((a,b))
a = torch.tensor([[1],[2],[3]])
b = torch.tensor([[4],[5],[6]])
output=torch.vstack((a,b))
print(output)


t = torch.arange(16.0).reshape(4,4)
output=torch.vsplit(t, 2)

output=torch.vsplit(t, [3, 6])

print(output)




x = torch.tensor([1, 2, 3, 4])
output=torch.unsqueeze(x, 0)
print(output)
output=torch.unsqueeze(x, 1)
print(output)



output=torch.unbind(torch.tensor([[1, 2, 3],
                           [4, 5, 6],
                           [7, 8, 9]]))


print(output)


x = torch.randn(2, 3)
x
output=torch.transpose(x, 0, 1)
print(output)


x = torch.tensor([1, 2, 3])
output=x.tile((2,2))
print(output)
 




x = torch.arange(8)
output=torch.tensor_split(x, 3)
print(output)

x = torch.arange(7)
torch.tensor_split(x, 3)
torch.tensor_split(x, (1, 6))

x = torch.arange(14).reshape(2, 7)
x
torch.tensor_split(x, 3, dim=1)
torch.tensor_split(x, (1, 6), dim=1)



src = torch.tensor([[4, 3, 5],
                    [6, 7, 8]])
output=torch.take(src, torch.tensor([0, 2, 5]))
print(output)




x = torch.zeros(2, 1, 2, 1, 2)

print(x.size())
y = torch.squeeze(x)
print(y.size())
y = torch.squeeze(x, 0)
print(y.size())
y = torch.squeeze(x, 1)
print(y.size())
y = torch.squeeze(x, (1, 2, 3))
print(y.size())





a = torch.arange(10).reshape(5, 2)

output=torch.split(a, 2)
print(output)
output=torch.split(a, [1, 4])
print(output)



a = torch.zeros(8, 8)
b = torch.ones(2, 8)
output=a.slice_scatter(b, start=6)
print(output)

b = torch.ones(8, 2)
output=a.slice_scatter(b, dim=1, start=2, end=6, step=2)




a = torch.zeros(2, 2)
b = torch.ones(2)
output=a.select_scatter(b, 1, 0)
print(output)




a = torch.zeros(3, 3)


output=torch.diagonal_scatter(a, torch.rand(3), 0)
print(output)

output=torch.diagonal_scatter(a, torch.ones(2), 1)

print(output)




a = torch.arange(4.)
torch.reshape(a, (2, 2))
b = torch.tensor([[0, 1], [2, 3]])
torch.reshape(b, (-1,))





x = torch.randn(2, 3, 5)
x.size()
output=torch.permute(x, (2, 0, 1))
print(output)




output=torch.nonzero(torch.tensor([1, 1, 1, 0, 1]))
print(output)
output=torch.nonzero(torch.tensor([[0.6, 0.0, 0.0, 0.0],
                            [0.0, 0.4, 0.0, 0.0],
                            [0.0, 0.0, 1.2, 0.0],
                            [0.0, 0.0, 0.0,-0.4]]))
print(output)
output=torch.nonzero(torch.tensor([1, 1, 1, 0, 1]), as_tuple=True)
print(output)
output=torch.nonzero(torch.tensor([[0.6, 0.0, 0.0, 0.0],
                            [0.0, 0.4, 0.0, 0.0],
                            [0.0, 0.0, 1.2, 0.0],
                            [0.0, 0.0, 0.0,-0.4]]), as_tuple=True)
print(output)
output=torch.nonzero(torch.tensor(5), as_tuple=True)
print(output)




x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
torch.narrow(x, 0, 0, 2)
torch.narrow(x, 1, 1, 2)
torch.narrow(x, -1, torch.tensor(-1), 1)



x = torch.randn(3, 4)

mask = x.ge(0.2)
output=torch.masked_select(x, mask)
print(output)


a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
output=torch.hstack((a,b))
#print(output)
a = torch.tensor([[1],[2],[3]])
b = torch.tensor([[4],[5],[6]])
output=torch.hstack((a,b))

print(output)


t = torch.arange(16.0).reshape(4,4)
t
output=torch.hsplit(t, 2)

output=torch.hsplit(t, [3, 2])

print(output)



t = torch.tensor([[1, 2], [3, 4]])
output=torch.gather(t, 1, torch.tensor([[0, 1], [0, 1]]))

print(output)




a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
output=torch.dstack((a,b))
print(output)
a = torch.tensor([[1],[2],[3]])
b = torch.tensor([[4],[5],[6]])
torch.dstack((a,b))





a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
output=torch.column_stack((a, b))
print(output)

a = torch.arange(5)
b = torch.arange(10).reshape(5, 2)
output=torch.column_stack((a, b, b))
print(output)




t = torch.arange(16.0).reshape(4, 4)
t
output=torch.dsplit(t, 2)
print(output)

torch.dsplit(t, [3, 6])



output=torch.arange(11).chunk(5)
print(output)

x = torch.randn(2, 3)
x
torch.cat((x, x, x), 0)
torch.cat((x, x, x), 1)






t = torch.tensor([1, 0, 1])
output=torch.argwhere(t)

print(output)
t = torch.tensor([[1, 0, 1], [0, 1, 1]])
output=torch.argwhere(t)
print(output)


real = torch.tensor([1, 2], dtype=torch.float32)
imag = torch.tensor([3, 4], dtype=torch.float32)
z = torch.complex(real, imag)

print(z)




torch.eye(3)


output=torch.empty((2,3), dtype=torch.int64)
print(output)

output=torch.full((2, 3), 3.141592)

print(output
torch.linspace(3, 10, steps=5)
torch.linspace(-10, 10, steps=5)
torch.linspace(start=-10, end=10, steps=5)
torch.linspace(start=-10, end=10, steps=1)



torch.arange(5)
torch.arange(1, 4)
torch.arange(1, 2.5, 0.5)

torch.range(1, 4)
torch.range(1, 4, 0.5)


output=torch.ones(5)
print(output)



output=torch.zeros_like(output)
print(output)

a = array.array('i', [1, 2, 3])
t = torch.frombuffer(a, dtype=torch.int32)

print(t)

# Interprets the signed char bytes as 32-bit integers.
# Each 4 signed char elements will be interpreted as
# 1 signed 32-bit integer.
import array
a = array.array('b', [-1, 0, 0, 0])
torch.frombuffer(a, dtype=torch.int32)



a = numpy.array([1, 2, 3])
t = torch.from_numpy(a)





t = torch.randn(2, 5, dtype=torch.float64)
t.numpy().tofile('storage.pt')
t_mapped = torch.from_file('storage.pt', shared=False, size=10, dtype=torch.float64)

print(t_mapped)






x = torch.randn(3, 3)
x
t = torch.as_strided(x, (2, 2), (1, 2))
t
t = torch.as_strided(x, (2, 2), (1, 2), 1)



a = numpy.array([1, 2, 3])
t = torch.as_tensor(a)
print(t)




a = torch.tensor([1, 2, 3])
# Shares memory with tensor 'a'
b = torch.asarray(a)
print(b)


torch.tensor([[0.1, 1.2], [2.2, 3.1], [4.9, 5.2]])

torch.tensor([0, 1])  # Type inference on data

torch.tensor(3.14159)  # Create a zero-dimensional (scalar) tensor

torch.tensor([])  # Create an empty tensor (of size (0,))



a = torch.randn(1, 2, 3, 4, 5)
print( torch.numel(a))
a = torch.zeros(4,4)
print(torch.numel(a))


torch.is_nonzero(torch.tensor([0.]))
torch.is_nonzero(torch.tensor([1.5]))
torch.is_nonzero(torch.tensor([False]))
torch.is_nonzero(torch.tensor([3]))
torch.is_nonzero(torch.tensor([1, 3, 5]))
torch.is_nonzero(torch.tensor([]))


x = torch.tensor([-1 + 1j, -2 + 2j, 3 - 3j])  
y = torch.conj(x)  

print(x.is_conj()) # 输出: False  
print( y.is_conj()) # 输出: True

print(y)




x = torch.tensor([1+1j])
output=torch.is_complex(x)
print(output)



x = torch.tensor([1, 2, 3])
output=torch.is_storage(x.untyped_storage())
print(output)



x = torch.tensor([1, 2, 3])
output=torch.is_tensor(x)
print(output)
'''